﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FaultBackService.BLL;

namespace FaultBackService.DataHelper
{
    /// <summary>
    /// 实时故障分析帮助类,综合十分钟风机数据、风场数据、故障定义数据，解析出实时故障，写入数据库
    /// </summary>
    class RealTimeFaultHelper
    {
        /// <summary>
        /// 风机十分钟数据
        /// </summary>
        internal IList<Models.t_TurbDataGenericViewModel> _tenMeanList;
        /// <summary>
        /// 风场十分钟数据平均值
        /// </summary>
        internal Models.TenMeanFarmData _farmMean;
        /// <summary>
        /// 实时故障定义字典
        /// </summary>
        internal IDictionary<int, Models.RealTimeFault> _dictRealFault;
        /// <summary>
        /// 风机字典
        /// </summary>
        internal IDictionary<int, Models.Turbine> _dictTurbine;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tenMeanList">十分钟数据列表</param>
        /// <param name="farmMean">风场平均值</param>
        public RealTimeFaultHelper(IList<Models.t_TurbDataGenericViewModel> tenMeanList, Models.TenMeanFarmData farmMean)
        {
            _tenMeanList = tenMeanList;
            _farmMean = farmMean;
            _dictRealFault = new RealTimeFault().GetDict();
            _dictTurbine = new Turbine().GetDict();
        }

        /// <summary>
        /// 故障分析，写入故障数据库,返回写入的故障数目
        /// </summary>
        public int WriteRealTimeFault()
        {
            //用以保存故障数据的列表
            IList<Models.RealTimeFaultData> faultDataList = new List<Models.RealTimeFaultData>();

            int faultLevel;

            //逐一分析十分钟平均数，如判断有故障，向faultDataList中添加对象
            foreach (var tenMean in _tenMeanList)
            {
                //分析GBox,GBox对应FaultId=1
                if (tenMean.GBoxDegC_Mean!=null && _farmMean.GBoxDegC_Mean!=null && _farmMean.GenDegC_Mean.GetValueOrDefault()!=0)
                {
                    float gbox_offsetPict = ((tenMean.GBoxDegC_Mean.GetValueOrDefault() - _farmMean.GBoxDegC_Mean.GetValueOrDefault()) / _farmMean.GBoxDegC_Mean.GetValueOrDefault()) * 100;  //计算百分比
                    faultLevel = GetRealTimeFaultLevel(1, gbox_offsetPict);
                    if (faultLevel > 0)   //GBox对应FaultId=1
                    {
                        var faultData = RealTimeFaultDataFactory
                            .Create(_dictRealFault[1], faultLevel, tenMean.NodeID, _dictTurbine[tenMean.NodeID].TurbineName,tenMean.TStampOfMidPoint,
                            gbox_offsetPict, tenMean.GBoxDegC_Mean.GetValueOrDefault(), _farmMean.GBoxDegC_Mean.GetValueOrDefault(), _farmMean.TenMeanFarmDataId);
                        faultDataList.Add(faultData);
                    }
                }

                //分析Gen,Gen对应FaultId=2
                if (tenMean.GenDegC_Mean!=null && _farmMean.GenDegC_Mean!=null && _farmMean.GenDegC_Mean.GetValueOrDefault()!=0)
                {
                    float gen_offsetPict = ((tenMean.GenDegC_Mean.GetValueOrDefault() - _farmMean.GenDegC_Mean.GetValueOrDefault()) / _farmMean.GenDegC_Mean.GetValueOrDefault()) * 100;
                    faultLevel = GetRealTimeFaultLevel(2, gen_offsetPict);
                    if (faultLevel > 0)   //Gen对应FaultId=2
                    {
                        var faultData = RealTimeFaultDataFactory
                            .Create(_dictRealFault[2], faultLevel, tenMean.NodeID, _dictTurbine[tenMean.NodeID].TurbineName, tenMean.TStampOfMidPoint,
                            gen_offsetPict, tenMean.GenDegC_Mean.GetValueOrDefault(),  _farmMean.GenDegC_Mean.GetValueOrDefault(), _farmMean.TenMeanFarmDataId);
                        faultDataList.Add(faultData);
                    }
                }


                
                
            }

            //将故障数据写入数据库,返回写入的故障条数
            if (faultDataList.Count > 0)
            {
                return new RealTimeFaultData().AddList(faultDataList);
            }
            else
            {
                return 0;
            }

        }


        /// <summary>
        /// 分析风机平均值和风场平均值的差值，是否匹配指定故障，匹配返回故障级别，不匹配返回0
        /// </summary>
        /// <param name="offsetPict"></param>
        public int GetRealTimeFaultLevel(int faultId, float offsetPict)
        {
            //实时故障定义信息
            var realFault = _dictRealFault[faultId];

            int faultLevel = 0;
            if (offsetPict >= realFault.FaultLevel3_OffsetPict)
            {
                faultLevel = 3;
            }
            else if (offsetPict >= realFault.FaultLevel2_OffsetPict)
            {
                faultLevel = 2;
            }
            else if (offsetPict >= realFault.FaultLevel1_OffsetPict)
            {
                faultLevel = 1;
            }

            return faultLevel;
        }
    }
}
